package com.xlh.Model;

import android.content.Context;
import android.widget.ListView;

import com.google.common.collect.Lists;
import com.xlh.Utils.DBManager;
import com.xlh.bean.DataBase.ButCmd;
import com.xlh.lib.Core.FFCursorHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fang on 2018-01-16.
 */

public class ButCmdsModel extends DBManager {

    public ButCmdsModel(Context context) {
        super(context);
        setTableName("button_cmds");
    }



    public List<ButCmd> getList() {

        FFCursorHelper csrHelper = new FFCursorHelper();
        List<ButCmd> datas = new ArrayList<ButCmd>();
        csrHelper.setCsr(
                getDb().rawQuery(String.format("select * from %s order by id desc", getTableName()), null)
        );

        if (csrHelper.getCsr().getCount() <= 0) {
            return null;
        } else {



            while (csrHelper.getCsr().moveToNext()) {
                ButCmd data = new ButCmd();
                data.setKey(csrHelper.getString("butkey"));
                data.setId(csrHelper.getInt("id"));
                data.setParentKey(csrHelper.getString("parent_but_key"));
                data.setCmds(csrHelper.getString("cmds"));
                datas.add(data);

            }
           // Lists.newArrayList(datas);
        }

        csrHelper.getCsr().close();

        return datas;
    }


    public void add(ButCmd cmd){
        getDb().execSQL(
                String.format("insert into %s (butkey,parent_but_key,cmds)values(?,?,?)",
                        getTableName()),
                        new Object[]{
                                cmd.getKey(),
                                cmd.getParentKey(),
                                cmd.getCmds()

                        }
        );
    }

    public ButCmd getByParentKeyAndButKey(String pk,String k){

        FFCursorHelper csrHelper = new FFCursorHelper();
        csrHelper.setCsr(
                getDb().rawQuery(
                        String.format("select * from %s where parent_but_key=? and butkey=?",
                                getTableName()),
                                new String[]{pk,k}
                )
        );

        if(csrHelper.getCsr().getCount()<=0) {
            csrHelper.getCsr().close();
            return null;
        }
        ButCmd data = new ButCmd();
        csrHelper.getCsr().moveToFirst();
        data.setKey(csrHelper.getString("butkey"));
        data.setId(csrHelper.getInt("id"));
        data.setParentKey(csrHelper.getString("parent_but_key"));
        data.setCmds(csrHelper.getString("cmds"));
        csrHelper.getCsr().close();
        return data;

    }

    /**
     * 根据PK和K进行更新
     * @param cmd
     */
    public void update(ButCmd cmd){

        getDb().execSQL(
                String.format("update %s set cmds=? where parent_but_key=? and butkey=?",
                        getTableName()),
                new String[]{cmd.getCmds(),cmd.getParentKey(),cmd.getKey()}
        );

    }

    /**
     * 根据PK和K进行删除
     * @param cmd
     */
    public void delete(ButCmd cmd){

        getDb().execSQL( String.format("delete from %s where parent_but_key=? and butkey=?", getTableName()),
                                        new String[]{cmd.getParentKey(),cmd.getKey()}
        );


    }



}




